package com.lzy.controller;

import com.lzy.base.BaseInfoProperties;
import com.lzy.grace.result.GraceJSONResult;
import com.lzy.grace.result.ResponseStatusEnum;
import com.lzy.pojo.Users;
import com.lzy.pojo.bo.ModifyUserBO;
import com.lzy.pojo.vo.UsersVO;
import com.lzy.service.UsersService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Modifier;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("userinfo")
public class UserController extends BaseInfoProperties {

    @Resource
    private UsersService usersService;

    @PostMapping("modify")
    public GraceJSONResult modify(@RequestBody ModifyUserBO userBO){
        // 修改用户信息
        usersService.modifyUserInfo(userBO);

        // 返回用户最新信息
        UsersVO usersVO = getUserInfo(userBO.getUserId(),true);
        return GraceJSONResult.ok(usersVO);
    }


    @PostMapping("get")
    public GraceJSONResult get(@RequestParam String userId){
        return GraceJSONResult.ok(getUserInfo(userId,false));
    }

    public UsersVO getUserInfo(String userId, boolean needToken) {

        // 获得最新的用户数据
        Users latestUser = usersService.getById(userId);
        UsersVO usersVO = new UsersVO();
        if(latestUser != null) BeanUtils.copyProperties(latestUser,usersVO);
        if(needToken){
            String utoken = TOKEN_USER_PREFIX + SYMBOL_DOT + UUID.randomUUID();
            //redis.set(REDIS_USER_TOKEN + ":" + userId,utoken);  // 设置分布式会话
            // 本方式允许用户在多台多设备进行登录
            redis.set(REDIS_USER_TOKEN + ":" + utoken,userId);  // 设置分布式会话
            usersVO.setUserToken(utoken);
        }
        return usersVO;
    }


    @PostMapping("updateFace")
    public GraceJSONResult updateFace(@RequestParam("userId") String userId,
                                  @RequestParam("face") String face){
        return GraceJSONResult.ok(commonDealUpdateUserInfo(userId,face));
    }

    @PostMapping("queryFriend")
    public GraceJSONResult queryFriend(@RequestParam("queryString") String queryString,
                                       HttpServletRequest httpServletRequest){
        if(StringUtils.isBlank(queryString)){
            return GraceJSONResult.errorMsg("请输入查询内容");
        }
        queryString = queryString.trim();
        Users friend = usersService.getByWechatNumOrMobile(queryString);
        if(friend == null){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FRIEND_NOT_EXIST_ERROR);
        }
        //获取调用url的用户id
        String userId = httpServletRequest.getHeader("headerUserId");
        if(friend.getId().equals(userId)){ //不能添加自己为好友
            return GraceJSONResult.errorCustom(ResponseStatusEnum.CAN_NOT_ADD_SELF_FRIEND_ERROR);
        }
        return GraceJSONResult.ok(friend);
    }


    @PostMapping("updateFriendCircleBg")
    public GraceJSONResult updateFriendCircleBg(@RequestParam("userId") String userId,
                                                @RequestParam("FriendCircleBg") String FriendCircleBg){
        return GraceJSONResult.ok(commonDealUpdateUserInfo(userId,FriendCircleBg));
    }


    @PostMapping("uploadChatBg")
    public GraceJSONResult uploadChatBg(@RequestParam("userId") String userId,
                                        @RequestParam("FriendCircleBg") String chatBg){
        return GraceJSONResult.ok(commonDealUpdateUserInfo(userId,chatBg));
    }

    private UsersVO commonDealUpdateUserInfo(String userId,String image){
        ModifyUserBO userBO = new ModifyUserBO();
        userBO.setUserId(userId);
        userBO.setChatBg(image);

        // 修改用户信息
        usersService.modifyUserInfo(userBO);

        // 返回用户最新信息
        UsersVO usersVO = getUserInfo(userBO.getUserId(),true);
        return usersVO;
    }
}
